package com.mobialia.socialmediademo;

import oauth.signpost.OAuth;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class LinkedInProviderActivity extends Activity implements Runnable {
	
	public static String TAG = "LinkedInProviderActivity";

	Thread thread;
	ProgressDialog loadingDialog;
	
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Toast toast = Toast.makeText(LinkedInProviderActivity.this, R.string.error, Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}
	};
	
    public void onResume() {
    	super.onResume();
    	
    	loadingDialog = ProgressDialog.show(this, "", getResources().getText(R.string.loading), true, true);
    	loadingDialog.show();
    	
    	thread = new Thread(this);
    	thread.start();
    }

	public void onPause() {
		super.onPause();
		loadingDialog.dismiss();
	}
	
	public void run() {
    	Uri uri = this.getIntent().getData();
    	
    	if (uri != null && uri.toString().startsWith(LinkedInProvider.CALLBACK_URL)) {
    		String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
    		Log.d(TAG, verifier);
    			
    		try {
				LinkedInProvider linkedInProvider = new LinkedInProvider(this);
    			linkedInProvider.verifyLogin(verifier, this);
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendEmptyMessage(0);
    		}
    	}
		Intent intent = new Intent(getApplicationContext(), SocialMediaDemo.class);
		startActivity(intent);
		finish();
	}
}